home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / DOSREF33.ZIP / CHAPTER.007 < prev    next >
Text File  |  1994-01-20  |  47KB  |  858 lines

  1.  
  2.    **  Programmer's Technical Reference for MSDOS and the IBM PC **
  3.             USA copyright TXG 392-616  ALL RIGHTS RESERVED
  4.  ──────────────────────────┤ DOSREF (tm) ├───────────────────────────
  5.                  ISBN 1-878830-02-3 (disk-based text)
  6.                 Copyright (c) 1987, 1994 Dave Williams
  7.                    ┌─────────────────────────────┐
  8.                    │ Shareware Version, 01/20/94 │
  9.                    │  Please Register Your Copy  │
  10.                    └─────────────────────────────┘
  11.  
  12.                       C H A P T E R    S E V E N
  13.  
  14.                           DOS FILE STRUCTURE
  15.  
  16.                             C O N T E N T S
  17.  
  18. File Management Functions ....................................... 7**1
  19. FCB Function Calls .............................................. 7**2
  20. Handle Function Calls ........................................... 7**3
  21. Special File Handles ............................................ 7**4
  22. Raw and Cooked File I/O ......................................... 7**5
  23. Number of Open Files Allowed  ................................... 7**6
  24. Restrictions on FCB Usage ....................................... 7**7
  25. Restrictions on Handle usage .................................... 7**8
  26. Allocating Space to a File ...................................... 7**9
  27. MSDOS / PCDOS Differences ....................................... 7**10
  28. .COM File Structure ............................................. 7**11
  29. .EXE File Structure ............................................. 7**12
  30. The Relocation Table ............................................ 7**13
  31. "NEW" .EXE Format (Microsoft Windows and OS/2) .................. 7**14
  32. Standard File Control Block ..................................... 7**15
  33. Extended File Control Block ..................................... 7**16
  34. Disk Transfer Area .............................................. 7**17
  35.  
  36.  
  37.  
  38. File Management Functions ....................................... 7**1
  39.  
  40.   Use DOS function calls to create, open, close, read, write, rename, 
  41. find, and erase files.  There are two sets of function calls that DOS 
  42. provides for support of file management. They are: 
  43.  
  44.    * File Control Block function calls   (0Fh-24h)
  45.    * Handle function calls               (39h-69h)
  46.  
  47.   Handle function calls are easier to use and are more powerful than 
  48. FCB calls. Microsoft recommends that the handle function calls be used 
  49. when writing new programs.  DOS 3.0 up have been curtailing use of FCB 
  50. function calls; it is possible that future versions of DOS may not 
  51. support FCB function calls. 
  52.  
  53.   The following table compares the use of FCB calls to Handle function 
  54. calls: 
  55.  
  56. ┌─────────────────────────────┬─────────────────────────────────────────┐
  57. │           FCB Calls         │               Handle Calls              │
  58. ├─────────────────────────────┼─────────────────────────────────────────┤
  59. │  Access files in current    │  Access files in ANY directory          │
  60. │  directory only.            │                                         │
  61. │                             │                                         │
  62. │  Requires the application   │  Does not require use of an FCB.        │
  63. │  program to maintain a file │  Requires a string with the drive,      │
  64. │  control block to open,     │  path, and filename to open, create,    │
  65. │  create, rename or delete   │  rename, or delete a file. For file     │
  66. │  a file. For I/O requests,  │  I/O requests, the application program  │
  67. │  the application program    │  must maintain a 16 bit file handle     │
  68. │  also needs an FCB          │  that is supplied by DOS.               │
  69. └─────────────────────────────┴─────────────────────────────────────────┘
  70.  
  71.   The only reason an application should use FCB function calls is to 
  72. maintain the ability to run under DOS 1.x.  To to this, the program 
  73. may use only function calls 00h-2Eh.  Though the FCB function calls 
  74. are frowned upon, many of the introductory assembly language 
  75. programming texts use the FCB calls as examples. 
  76.  
  77.   PC-MOS/386 supports the FCB calls but recommends using the handle 
  78. calls. 
  79.  
  80.  
  81.  
  82. FCB Function Calls .............................................. 7**2
  83.  
  84.   FCB function calls require the use of one File Control Block per 
  85. open file, which is maintained by the application program and DOS.  
  86. The application program supplies a pointer to the FCB and fills in the 
  87. appropriate fields required by the specific function call.  An FCB 
  88. function call can perform file management on any valid drive, but only 
  89. in the current logged directory.  By using the current block, current 
  90. record, and record length fields of the FCB, you can perform 
  91. sequential I/O by using the sequential read or write function calls.  
  92. Random I/O can be performed by filling in the random record and record 
  93. length fields. 
  94.  
  95.   Several possible uses of FCB type calls are considered programming 
  96. errors and should not be done under any circumstances to avoid 
  97. problems with file sharing and compatibility with later versions of 
  98. DOS. 
  99.  
  100.  Some errors are:
  101.  
  102. 1) If program uses the same FCB structure to access more than one open 
  103.    file. By opening a file using an FCB, doing I/O, and then replacing 
  104.    the filename field in the file control block with a new filename, a 
  105.    program can open a second file using the same FCB. This is invalid 
  106.    because DOS writes control information about the file into the 
  107.    reserved fields of the FCB. If the program replaces the filename 
  108.    field with the original filename and then tries to perform I/O on 
  109.    this file, DOS may become confused because the control information 
  110.    has been changed. An FCB should never be used to open a second file 
  111.    without closing the one that is currently open. If more than one 
  112.    File Control Block is to be open concurrently, separate FCBs should 
  113.    be used. 
  114.  
  115. 2) A program should never try to use the reserved fields in the FCB, 
  116.    as the function of the fields may change with different versions of 
  117.    DOS. 
  118.  
  119. 3) A delete or a rename on a file that is currently open is considered 
  120.    an error and should not be attempted by an application program. 
  121.  
  122.   It is also good programming practice to close all files when I/O is 
  123. done. This avoids potential file sharing problems that require a limit 
  124. on the number of files concurrently open using FCB function calls. 
  125.  
  126.  
  127.  
  128. Handle Function Calls ........................................... 7**3
  129.  
  130.   The recommended method of file management is by using the extended 
  131. "handle" set of function calls.  These calls are not restricted to the 
  132. current directory.  Also, the handle calls allow the application 
  133. program to define the type of access that other processes can have 
  134. concurrently with the same file if the file is being shared. 
  135.  
  136.   To create or open a file, the application supplies a pointer to an 
  137. ASCIIZ string giving the name and location of the file.  The ASCIIZ 
  138. string contains an optional drive letter, optional path, mandatory 
  139. file specification, and a terminal byte of 00h.  The following is an 
  140. example of an ASCIIZ string: 
  141.  
  142.      format:          [drive][path] FILENAME.EXT,0
  143.  
  144.      in MASM:         db "A:\PATH\FILENAME.EXT",0
  145.  
  146.   If the file is being created, the application program also supplies 
  147. the attribute of the file.  This is a set of values that defines the 
  148. file read-only, hidden, system, directory, or volume label. 
  149.  
  150.   If the file is being opened, the program can define the sharing and 
  151. access modes that the file is opened in.  The access mode informs DOS 
  152. what operations your program will perform on this file (read-only, 
  153. write-only, or read/write).  The sharing mode controls the type of 
  154. operations other processes may perform concurrently on the file.  A 
  155. program can also control if a child process inherits the open files of 
  156. the parent.  The sharing mode has meaning only if file sharing is 
  157. loaded when the file is opened. 
  158.  
  159.   To rename or delete a file, the appplication program simply needs to 
  160. provide a pointer to the ASCIIZ string containing the name and 
  161. location of the file and another string with the new name if the file 
  162. is being renamed. 
  163.  
  164.   The open or create function calls return a 16-bit value referred to 
  165. as the file handle.  To do any I/O to a file, the program uses the 
  166. handle to reference the file.  Once a file is opened, a program no 
  167. longer needs to maintain the ASCIIZ string pointing to the file, nor 
  168. is there any need to stay in the same directory.  DOS keeps track of 
  169. the location of the file regardless of what directory is current. 
  170.  
  171.   Sequential I/O can be performed using the handle read (3Fh) or write 
  172. (40h) function calls.  The offset in the file that I/O is performed to 
  173. is automatically moved to the end of what was just read or written.  
  174. If random I/O is desired, the LSEEK (42h) function call can be used to 
  175. set the offset into the file where I/O is to be performed. 
  176.  
  177.  
  178.  
  179. Special File Handles ............................................ 7**4
  180.  
  181.   DOS reserves five special file handles for use by itself and 
  182. applications programs. They are: 
  183.  
  184. ┌───────┬────────┬──────────────────────────────────────────────────────┐
  185. │ 0000h │ STDIN  │ standard input device   (input can be redirected)    │
  186. │ 0001h │ STDOUT │ standard output device  (output can be redirected)   │
  187. │ 0002h │ STDERR │ standard error output device (output cannot be       │
  188. │       │        │                               redirected)            │
  189. │       │        │ NOTE: DOS opens STDERR for both writing and reading. │
  190. │       │        │  Since STDIN can be redirected, using STDERR to read │
  191. │       │        │  the keyboard is a reliable way to ensure that your  │
  192. │       │        │  program is actually                                 │
  193. │       │        │ reading the keyboard, if that's what you want to do. │
  194. │ 0004h │ STDAUX │ standard auxiliary device                            │
  195. │ 0005h │ STDPRN │ standard printer device (PRN, normally LPT1)         │
  196. └───────┴────────┴──────────────────────────────────────────────────────┘
  197.  
  198.   These handles are predefined by DOS and can be used by an 
  199. application program.  They do not need to be opened by a program, 
  200. although a program can close these handles.  STDIN should be treated 
  201. as a read-only file, and STDOUT and STDERR should be treated as write-
  202. only files.  STDIN and STDOUT can be redirected.  All handles 
  203. inherited by a process can be redirected, but not at the command line. 
  204.  
  205.   These handles are very useful for doing I/O to and from the console 
  206. device.  For example, you could read input from the keyboard using the 
  207. read (3Fh) function call and file handle 0000h (STDIN), and write 
  208. output to the console screen with the write function call (40h) and 
  209. file handle 0001h (STDOUT).  If you wanted an output that could not be 
  210. redirected, you could output it using file handle 0002h (STDERR).  
  211. This is very useful for error messages that must be seen by a user. 
  212.  
  213.   File handles 0003h (STDAUX) and 0004h (STDPRN) can be both read from 
  214. and written to.  STDAUX is typically a serial device and STDPRN is 
  215. usually a parallel device. 
  216.  
  217.   DOS 2.0 through 3.21 were limited to 20 file handles.  This limited 
  218. application programs to 15 simultaneous handles.  DOS 3.3 and higher 
  219. added the int 21h/ Set Handle Count function to give up to 65,535 file 
  220. handles per application.  PC-MOS/386 can have more than 65,535 
  221. handles. 
  222.  
  223.  
  224. Raw and Cooked File I/O ......................................... 7**5
  225.  
  226.   Raw and cooked modes originated in the Unix world and were provided 
  227. with DOS 2.x+.  They apply only to character I/O (including the 
  228. keyboard, screen, printer and serial ports - but not block devices 
  229. like disk drives), and only to the "new" 2.x file handle I/O functions 
  230. (not the old FCB file I/O functions).  Raw mode is called "binary" 
  231. mode in DOS 3.x+, and cooked mode is called "ASCII."  The common raw-
  232. cooked convention is from DOS 2.x and other operating systems. 
  233.  
  234.   The five predefined DOS file handles are all devices, so the mode 
  235. can be changed from raw to cooked via IOCTL.  These handles are in 
  236. cooked mode when initialized by DOS.  Regular file handles that are 
  237. not devices are always in raw mode and cannot be changed to cooked 
  238. mode. 
  239.  
  240.   The predefined file handles STDIN (0000h) and STDOUT (0001h) and 
  241. STDERR (0002h) are all duplicate handles.  If the IOCTL function call 
  242. is used to change the mode of any of these three handles, the mode of 
  243. all three handles is changed.  For example, if IOCTL was used to 
  244. change STDOUT to raw, then STDIN and STDERR would also be changed to 
  245. raw mode. 
  246.  
  247.   In the default cooked mode, DOS examines the character I/O data 
  248. stream for certain special control characters, and takes specific 
  249. actions if they are found.  For example, Ctrl-C is treated as a Break 
  250. interrupt, Ctrl-S pauses the screen display, and Ctrl-Z is treated as 
  251. end-of-file. (If you try to send Ctrl-Z to a printer through a DOS 
  252. file handle in cooked mode, DOS closes the printer file!)  Also, input 
  253. is buffered within DOS until a CR is detected - so you can't process 
  254. each key as it is pressed. 
  255.  
  256.   In raw mode, DOS ignores special characters, passing them through 
  257. without any special processing, and does not buffer input lines.  So 
  258. to use file handle I/O to send bit-mapped graphics to a printer 
  259. through DOS, or process individual keystrokes immediately, or bypass 
  260. Ctrl-C checking, you need to switch the file handle to raw mode.  Raw 
  261. mode is not automatically reset to cooked mode by DOS when a program 
  262. terminates, so it is a good idea to reset the file into cooked mode 
  263. before your program exits if the system was in cooked mode to begin 
  264. with. I/O to files is done in raw mode. 
  265.  
  266.   To set a file handle into raw mode or back into cooked mode, use DOS 
  267. IOCTL (int 21h Fn 44h, Chapter 4): 
  268.  
  269.    1.  Get the current mode bits (Subfunction 0).
  270.    2.  Check that the file is a character file.  (If not, exit.)
  271.    3.  Switch the cooked mode bit to raw or vice versa.
  272.    4.  Set the mode bits (Subfunction 1).
  273.  
  274.   Microsoft C v4 and later do NOT set raw mode for binary files.  When 
  275. running with the CON driver set to raw mode (to enhance display speed) 
  276. programs compiled in MSC will crash the computer.  A letter to 
  277. Microsoft reporting this odd behavior got the somewhat bizarre reply 
  278. that "Microsoft does not support the use of any TSRs" from their 
  279. techs.  Raw mode is clearly documented by both IBM and Microsoft, and 
  280. their own tools should take it into account. 
  281.  
  282.  
  283.  
  284. FILE I/O IN BINARY (RAW) MODE
  285.  
  286. The following is true when a file is read in binary mode:
  287.  
  288. 1)  The characters ^S (scroll lock), ^P (print screen), ^C (control
  289.     break) are not checked for during the read.  Therefore, no printer
  290.     echo occurs if ^S or ^P are read.
  291. 2)  There is no echo to STDOUT (0001h).
  292. 3)  Read the number of specified bytes and returns immediately when the
  293.     last byte is received or the end of file reached.
  294. 4)  Allows no editing of the input using the function keys if the input
  295.     is from STDIN (0000h).
  296.  
  297.  
  298. The following is true when a file is written to in binary mode:
  299.  
  300. 1)  The characters ^S (scroll lock), ^P (print screen), ^C (control
  301.     break) are not checked for during the write.  Therefore, no printer
  302.     echo occurs.
  303. 2)  There is no echo to STDOUT (0001h).
  304. 3)  The exact number of bytes specified are written.
  305. 4)  Does not caret (^) control characters. For example, Ctrl-D is sent
  306.     out as byte 04h instead of the two bytes ^ and D.
  307. 5)  Does not expand tabs into spaces.
  308.  
  309.  
  310. FILE I/O IN ASCII (COOKED) MODE
  311.  
  312. The following is true when a file is read in ASCII mode:
  313.  
  314. 1)  Checks for the characters ^C,^S, and ^P.
  315. 2)  Returns as many characters as there are in the device input buffer,
  316.     or the number of characters requested, whichever is less.  If the
  317.     number of characters requested was less than the number of
  318.     characters in the device buffer, then the next read will address
  319.     the remaining characters in the buffer.
  320. 3)  If there are no more bytes remaining in the device input buffer,
  321.     read a line (terminated by a CR) into the buffer.  This line may
  322.     be edited with the function keys.  The characters return terminated
  323.     with a sequence of 0Dh, 0Ah (CR, LF) if the number of characters
  324.     requested is sufficient to include them.  For example, if 5
  325.     characters were requested, and only 3 were entered before the
  326.     carriage return (0Dh or ^M) was presented to DOS from the console
  327.     device, then the 3 characters entered and 0Dh and 0Ah would be
  328.     returned.  However, if 5 characters were requested and 7 were
  329.     entered before the carriage return, only the first 5 characters
  330.     would be returned.  No 0Dh, 0Ah sequence would be returned in this
  331.     case.  If less than the number of characters requested are entered
  332.     when the carriage return is received, the characters received and
  333.     0Dh, 0Ah would be returned.  The reason the 0Ah (linefeed or ^J)
  334.     is added to the returned characters is to make the devices look
  335.     like text files.
  336. 4)  If a 1Ah (^Z) is found, the input is terminated at that point.
  337.     No 0Dh, 0Ah (CR,LF) sequence is added to the string.
  338. 5)  Echoing is performed.
  339. 6)  Tabs are expanded.
  340.  
  341.  
  342. The following is true when a file is written to in ASCII mode:
  343.  
  344. 1)  The characters ^S,^P,and ^C are checked for during the write
  345.     operation.
  346. 2)  Expands tabs to 8-character boundaries and fills with spaces
  347.     (20h).
  348. 3)  Carets indicate control chars, for example, ^D is written as
  349.     two bytes, '^' and 'D'.
  350. 4)  Bytes are output until the number specified is output or a ^Z is
  351.     encountered.  The number actually output is returned to the user.
  352.  
  353.  
  354.  
  355. Number of Open Files Allowed  ................................... 7**6
  356.  
  357.   The number of files that can be open concurrently is restricted by 
  358. DOS.  This number is determined by how the file is opened or created 
  359. (FCB or handle function call) and the number specified by the FCBS and 
  360. FILES commands in the CONFIG.SYS file.  The number of files allowed 
  361. open by FCB function calls and the number of files that can be opened 
  362. by handle type calls are independent of one another. 
  363.  
  364.  
  365.  
  366. Restrictions on FCB Usage ....................................... 7**7
  367.  
  368.   If file sharing is not loaded using the SHARE command, there is no 
  369. restriction on the number of files concurrently open using FCB 
  370. function calls. 
  371.  
  372.   However, when file sharing is loaded, the maximum number of FCBs 
  373. open is set by the the FCBS command in the CONFIG.SYS file. 
  374.  
  375.   The FCBS command has two values you can specify, 'm' and 'n'.  The 
  376. value for 'm' specifies the number of files that can be opened by 
  377. FCBs, and the value 'n' specifies the number of FCBs that are 
  378. protected from being closed. 
  379.  
  380.   When the maximum number of FCB opens is exceeded, DOS automatically 
  381. closes the least recently used file.  Any attempt to access this file 
  382. results in an int 24h critical error message "FCB not available".  If 
  383. this occurs while an application program is running, the value 
  384. specified for 'm' in the FCBS command should be increased. 
  385.  
  386.   When DOS determines the least recently used file to close, it does 
  387. not include the first 'n' files opened, therefore the first 'n' files 
  388. are protected from being closed. 
  389.  
  390.  
  391.  
  392. Restrictions on Handle usage .................................... 7**8
  393.  
  394.   The number of files that can be open simultaneously by all processes 
  395. is determined by the FILES command in the CONFIG.SYS file.  The number 
  396. of files a single process can open depends on the value specified for 
  397. the FILES command.  If FILES is greater than or equal to 20, a single 
  398. process can open 20 files.  If FILES is less than 20, the process can 
  399. open less than 20 files. This value includes the three predefined 
  400. handles STDIN, STDOUT, and STDERR.  This means only 17 additional 
  401. handles can be added. DOS 3.3+ includes a function to use more than 20 
  402. files per application. 
  403.  
  404.  
  405.  
  406. Allocating Space to a File ...................................... 7**9
  407.  
  408.   Files are not necessarily written sequentially on a disk.  Space is 
  409. allocated as needed and the next location available on the disk is 
  410. allocated as space for the next file being written.  Therefore, if 
  411. considerable file generation has taken place, newly created files will 
  412. not be written in sequential sectors. However, due to the mapping 
  413. (chaining) of file space via the File Allocation Table (FAT) and the 
  414. function calls available, any file may be used in either a sequential 
  415. or random manner. 
  416.  
  417.   Space is allocated in increments called clusters.  Cluster size 
  418. varies according to the media type.  An application program should not 
  419. concern itself with the way that DOS allocates space to a file.  The 
  420. size of a cluster is only important in that it determines the smallest 
  421. amount of space that can be allocated to a file.  A disk is considered 
  422. full when all clusters have been allocated to files. 
  423.  
  424.   A DOS file can be up to (2^32)-1 (4,294,967,295) bytes long.  This 
  425. is the maximum value that can be put in the dword in the FAT which 
  426. holds the file size information, less one byte for the end of file 
  427. marker. 
  428.  
  429.  
  430.  
  431. MSDOS / PCDOS Differences ....................................... 7**10
  432.                                                 
  433.   There is a problem of compatibility between MS-DOS and IBM PC-DOS 
  434. having to do with FCB Open and Create.  The IBM 1.0, 1.1, and 2.0 
  435. documentation of OPEN (call 0Fh) contains the following statement: 
  436.  
  437.   "The current block field (FCB bytes C-D) is set to zero [when an FCB 
  438. is opened]." 
  439.  
  440.   This statement is NOT true of MS-DOS 1.25 or MS-DOS 2.00.  The 
  441. difference is intentional, and the reason is CP/M 1.4 compatibility.  
  442. Zeroing that field is not CP/M compatible.  Some CP/M programs will 
  443. not run when machine translated if that field is zeroed.  The reason 
  444. it is zeroed in the IBM versions is that IBM specifically requested 
  445. that it be zeroed.  This was the reason for the complaints from some 
  446. vendors about the fact that IBM MultiPlan would not run under MS-DOS. 
  447. It is probably the reason that some other very old IBM programs didn't
  448. run under MS-DOS. 
  449.  
  450. NOTE: Do what all MS/PC-DOS systems programs do: Set every single FCB 
  451. field you want to use regardless of what the documentation says is 
  452. initialized. 
  453.  
  454.  
  455.  
  456. .COM File Structure ............................................. 7**11
  457.  
  458.   The COM file structure was designed for DOS 1.0 and maximum 
  459. compatibility with programs ported from the CP/M operating system.  
  460. COM files normally comprise one segment only.  A COM file is loaded as 
  461. a memory image of the disk file and the Instruction Pointer is set to 
  462. offset 100h within the program. 
  463.  
  464.   The BIN files generated by EXE2BIN are COM files.
  465.  
  466.  
  467.  
  468. .EXE File Structure ............................................. 7**12
  469.  
  470.   The EXE file is the native mode for DOS.  EXE files may make use of 
  471. multiple segments for code, stack, and data.  The design of the EXE 
  472. file reflects the segmented design of the Intel 80x86 CPU 
  473. architecture.  EXE files may be as large as available memory and may 
  474. make references to specific segment addresses. 
  475.  
  476.   The EXE files produced by the Microsoft linker program consist of 
  477. two parts, control and relocation information and the load module 
  478. itself. 
  479.  
  480.   The control and relocation information, which is described below, is 
  481. at the beginning of the file in an area known as the header.  The load 
  482. module immediately follows the header.  The load module begins in the 
  483. memory image of the module contructed by the Microsoft linker. 
  484.  
  485.   When you are loading a file with the name *.EXE, DOS does NOT assume 
  486. that it is an EXE format file.  It looks at the first two bytes for a 
  487. signature (the letters MZ) telling it that it is an EXE file.  If it 
  488. has the proper signature, then the load proceeds.  Otherwise, it 
  489. presumes the file to be a .COM format file. 
  490.  
  491.   If the file has the EXE signature, then the internal consistency is 
  492. checked.  Pre-2.0 versions of MSDOS did not check the signature byte 
  493. for EXE files. 
  494.  
  495.   The .EXE format can support programs larger than 64K.  It does this 
  496. by allowing separate segments to be defined for code, data, and the 
  497. stack, each of which can be up to 64K long.  Programs in EXE format 
  498. may contain explicit references to segment addresses.  A header in the 
  499. EXE file has information for DOS to resolve these references. 
  500.  
  501.   EXE file size does not reflect the amount of RAM an EXE file might 
  502. use, since stack space or extra RAM may be allocated by the EXE loader 
  503. at init.  Some programs may also store their overlay files in the main 
  504. EXE file. 
  505.  
  506.   EXE files produced by the Microsoft linker are considered to be 
  507. standard EXEs.  Digital Research's RASM86 linker produces quite 
  508. different EXE files, as do the linkers from SLR Systems and Phoenix.  
  509. Intel's original linker for their 8086 compiler is much more 
  510. sophisticated than the Microsoft linker, which originally was a simple 
  511. subset of the Intel linker.  Unfortunately the new fields added by 
  512. OS/2, Windows, and CodeView make the newer Microsoft linkers somewhat 
  513. different from the original Intel linker. 
  514.  
  515.  
  516. ┌───────────────────────────────────────────────────────────────────────┐
  517. │                     E X E   F I L E   H E A D E R                     │
  518. ├─────────┬──────┬──────────────────────────────────────────────────────┤
  519. │ Offset  │ Size │                 C O N T E N T S                      │
  520. ├─────────┼──────┼─────┬────────────────────────────────────────────────┤
  521. │   00h   │ BYTE │ 4Dh │ The Linker's signature to mark the file as a   │
  522. ├─────────┼──────┼─────┤ valid .EXE file (ASCII letters M and Z, for    │
  523. │   01h   │ BYTE │ 5Ah │ Mark Zbikowski, one of the major DOS           │
  524. │         │      │     │ programmers at Microsoft)                      │
  525. ├─────────┼──────┼─────┴────────────────────────────────────────────────┤
  526. │ 02h-03h │ WORD │ Length of the image mod 512 (remainder after dividing│
  527. │         │      │ the load module image size by 512) (including header)│
  528. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  529. │ 04h-05h │ WORD │ Size of the file in 512 byte pages including the     │
  530. │         │      │ header.                                              │
  531. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  532. │ 06h-07h │ WORD │ Number of relocation table items following the header│
  533. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  534. │ 08h-09h │ WORD │ Size of the header in 16 byte (paragraphs).  This is │
  535. │         │      │ used to locate the beginning of the load module in   │
  536. │         │      │ the file.  Although the DOS loader will allow the    │
  537. │         │      │ actual program to begin at any location specified by │
  538. │         │      │ this value some debuggers, including Microsoft's,    │
  539. │         │      │ need the program to be aligned on a 512-byte boundary│
  540. │         │      │ in the .EXE file.                                    │
  541. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  542. │ 0Ah-0Bh │ WORD │ Minimum number of 16 byte paragraphs required above  │
  543. │         │      │ the end of the loaded program.                       │
  544. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  545. │ 0Ch-0Dh │ WORD │ Max number of 16 byte paragraphs required above the  │
  546. │         │      │ end of the loaded program. If the minimum and maximum│
  547. │         │      │ number of paragraphs are both zero, the program will │
  548. │         │      │ be loaded as high in memory as possible.             │
  549. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  550. │ 0Eh-0Fh │ WORD │ Displacement in paragraphs of stack segment within   │
  551. │         │      │ load module.  This size must be adjusted by          │
  552. │         │      │ relocation.                                          │
  553. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  554. │ 10h-11h │ WORD │Offset to be in SP register when the module is given  │
  555. │         │      │ control  (stack offset)                              │
  556. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  557. │ 12h-13h │ WORD │ Checksum - 16-bit negative sum of all the words in   │
  558. │         │      │ the file, ignoring overflow.                         │
  559. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  560. │ 14h-15h │ WORD │ Offset for the IP register when the module is given  │
  561. │         │      │ control  (initial instruction pointer)               │
  562. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  563. │ 16h-17h │ WORD │ Offset in paragraphs of code segment (CS) within load│
  564. │         │      │ module.  This size must be adjusted by relocation.   │
  565. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  566. │ 18h-19h │ WORD │ Offset in bytes of the relocation pointer table.     │
  567. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  568. │ 1Ah-1Bh │ WORD │ Overlay number (0 for the resident part of the       │
  569. │         │      │ program)                                             │
  570. ├─────────┼──────┼──────────────────────────────────────────────────────┤
  571. │   1Ch   │ BYTE │ (undocumented) Microsoft compilers put 01h here,     │
  572. │         │      │ which may indicate the version of .EXE header        │
  573. │         │      │ structure for later changes.  Normally the relocation│
  574. │         │      │ table begins at 1Eh, but that can be changed to a    │
  575. │         │      │ different location if the word at 18-19h is adjusted │
  576. │         │      │ to match.                                            │
  577. └─────────┴──────┴──────────────────────────────────────────────────────┘
  578.  
  579.  
  580.  
  581. The Relocation Table ............................................ 7**13
  582.  
  583.   The word at 18h locates the first entry in the relocation table.  
  584. The relocation table is made up of a variable number of relocation 
  585. items.  The number of items is contained at offset 06h.  The 
  586. relocation item contains two fields - a 2 byte offset value, followed 
  587. by a 2 byte segment value.  These two fields represent the 
  588. displacement into the load module before the module is given control.  
  589. The process is called relocation and is accomplished as follows: 
  590.  
  591. 1. The formatted part of the header is read into memory.  Its size
  592.    is 1Bh.
  593.  
  594. 2. A portion of memory is allocated depending on the size of the load 
  595.    module and the allocation numbers in offsets 0Ah and 0Ch. DOS 
  596.    always tries to allocate 0FFFFh paragraphs. Since this call will 
  597.    always fail, the function returns the amount of free memory. If 
  598.    this block is larger than the minimum specified at offset 0Ah and 
  599.    the loaded program size, DOS will allocate the size specified at 
  600.    offset 0Ch or the largest free memory space, whichever is less. 
  601.  
  602. 3. A Program Segment Prefix is built following the resident portion of 
  603.    the program that is performing the load operation. 
  604.  
  605. 4. The formatted part of the header is read into memory (its size is 
  606.    at offset 08h) 
  607.  
  608. 5. The load module size is determined by subtracting the header size 
  609.    from the file size. Offsets 04h and 08h can be used for this 
  610.    calculation. The actual size is downward adjusted based on the 
  611.    contents of offset 02h. Note that all files created by the Linker 
  612.    programs prior to version 1.10 always placed a value of 4 at this 
  613.    location, regardless of the actual program size. Therefore, 
  614.    Microsoft recommends that this field be ignored if it contains a 
  615.    value of 4. Based on the setting of the high/low loader switch, an 
  616.    appropriate segment is determined for loading the load module. This 
  617.    segment is called the start segment. 
  618.  
  619. 6. The load module is read into memory beginning at the start segment. 
  620.    The relocation table is an ordered list of relocation items. The 
  621.    first relocation item is the one that has the lowest offset in the 
  622.    file. 
  623.  
  624. 7. The relocation table items are read into a work area one or more at 
  625.    a time. 
  626.  
  627. 8. Each relocation table item segment value is added to the start 
  628.    segment value. The calculated segment, in conjunction with the 
  629.    relocation item offset value, points to a word in the load module 
  630.    to which is added the start segment value. The result is placed 
  631.    back into the word in the load module. 
  632.  
  633. 9. Once all the relocation items have been processed, the SS and SP 
  634.    registers are set from the values in the header and the start 
  635.    segment value is added to SS. The ES and DS registers are set to 
  636.    the segment address of the program segment prefix. The start 
  637.    segment value is added to the header CS register value. The result, 
  638.    along with the header IP value, is used to give the module control. 
  639.  
  640.  
  641.  
  642. "NEW" .EXE Format (Microsoft Windows and OS/2) .................. 7**14
  643.  
  644.   The "old" EXE format is documented here.  The "new" EXE format puts 
  645. more information into the header section and is currently used in 
  646. applications that run under Microsoft Windows.  The linker that 
  647. creates these files comes with the Microsoft Windows Software 
  648. Development Kit and is called LINK4.  If you try to run a Windows-
  649. linked program under DOS, you will get the error message "This program 
  650. requires Microsoft Windows".  The OS/2 1.x file format is essentially 
  651. the same as the Windows format. 
  652.  
  653.   Windows executables have dynamic linking and all of the code/data 
  654. inside an EXE file is not necessarily loaded at run time. 
  655.  
  656.   Offset 3Ch dword: offset of new .EXE header (for Microsoft Windows & 
  657. OS/2) 
  658.  
  659.  
  660.  
  661. Standard File Control Block ..................................... 7**15
  662.  
  663.   The standard file control block is defined as follows, with offsets 
  664. in hex: 
  665.  
  666. ┌───────────────────────────────────────────────────────────────────────┐
  667. │              F I L E      C O N T R O L      B L O C K                │
  668. ├───────┬─────────┬─────────────────────────────────────────────────────┤
  669. │offset │  size   │                      Function                       │
  670. ├───────┼─────────┼─────────────────────────────────────────────────────┤
  671. │   0   │ 1 byte  │ Drive number. For example:                          │
  672. │       ├─────────┴─────────────────────────────────────────────────────┤
  673. │       │ Before open:    00h = default drive                           │
  674. │       │                 01h = drive A:                                │
  675. │       │                 02h = drive B: etc.                           │
  676. │       │ After open:     00h = drive C:                                │
  677. │       │                 01h = drive A:                                │
  678. │       │                 02h = drive B: etc.                           │
  679. │       │ A zero is replaced by the actual drive number during opening. │
  680. ├───────┼─────────┬─────────────────────────────────────────────────────┤
  681. │  1-8  │ 8 bytes │ Filename, left justified with blanks.               │
  682. │       ├─────────┴─────────────────────────────────────────────────────┤
  683. │       │ If a reserved device name is placed here (such as PRN) do not │
  684. │       │ include the optional colon.                                   │
  685. ├───────┼─────────┬─────────────────────────────────────────────────────┤
  686. │  9-B  │ 3 bytes │ Filename extension, left justified with trailing    │
  687. │       │         │ blanks.                                             │
  688. ├───────┼─────────┼─────────────────────────────────────────────────────┤
  689. │  C-D  │ 2 bytes │ Current block # relative to start of file, starting │
  690. │       │         │ with 0                                              │
  691. │       ├─────────┴─────────────────────────────────────────────────────┤
  692. │       │ (set to 0 by the OPEN function call).  A block consists of    │
  693. │       │ 128 records, each of the size specified in the logical record │
  694. │       │ size field.  The current block number is used with the current│
  695. │       │ record field (below) for sequential reads and writes.         │
  696. ├───────┼─────────┬─────────────────────────────────────────────────────┤
  697. │  E-F  │ 2 bytes │ Logical record size in bytes.                       │
  698. │       ├─────────┴─────────────────────────────────────────────────────┤
  699. │       │ Set to 80h by OPEN function.  If this is not correct, you     │
  700. │       │ must set the value because DOS uses it to determine the       │
  701. │       │ proper locations in the file for all disk reads and writes.   │
  702. ├───────┼─────────┬─────────────────────────────────────────────────────┤
  703. │ 10-13 │ 4 bytes │ File size in bytes.                                 │
  704. │       ├─────────┴─────────────────────────────────────────────────────┤
  705. │       │ In this field, the first word is the low-order part of the    │
  706. │       │ size.                                                         │
  707. ├───────┼─────────┬─────────────────────────────────────────────────────┤
  708. │ 14-15 │ 2 bytes │Date file was created or last updated.               │
  709. │       ├─────────┴─────────────────────────────────────────────────────┤
  710. │       │ MM/DD/YY are mapped as follows:                               │
  711. │       │         15  14  13  12  11  10  9  8  7  6  5  4  3  2  1  0  │
  712. │       │         y   y   y   y   y   y   y  m  m  m  m  d  d  d  d  d  │
  713. │       │ where:            mm is 1-12                                  │
  714. │       │                   dd is 1-31                                  │
  715. │       │                   yy is 0-119 (1980-2099)                     │
  716. ├───────┼────────┬──────────────────────────────────────────────────────┤
  717. │ 16-17 │ 2 bytes│ Time file was created or last updated.               │
  718. ├───────┼────────┴──────────────────────────────────────────────────────┤
  719. │       │ These bytes contain the time when the file was created or     │
  720. │       │ last updated.  The time is mapped in the bits as follows:     │
  721. │       ├───────────────────────────────┬───────────────────────────────┤
  722. │       │         B Y T E   16h         │         B Y T E   17h         │
  723. │       ├───────────────────────────────┼───────────────────────────────┤
  724. │       │ F   E   D   C   B   A   9   8 │ 7   6   5   4   3   2   1   0 │
  725. │       ├───────────────────┬───────────┴───────────┬───────────────────┤
  726. │       │ H   H   H   H   H │ M   M   M   M   M   M │ D   D   D   D   D │
  727. │       ├───────────────────┼───────────────────────┼───────────────────┤
  728. │       │ binary # hrs 0-23 │ binary # minutes 0-59 │ bin. # 2-sec incr │
  729. │       ├───────────────────┴───────────────────────┴───────────────────┤
  730. │       │ note: The time is stored with the least significant byte first│
  731. ├───────┼────────┬──────────────────────────────────────────────────────┤
  732. │ 18-19 │ 2 bytes│ Reserved for DOS.                                    │
  733. ├───────┼────────┼──────────────────────────────────────────────────────┤
  734. │  20   │ 1 byte │ Current relative record number.                      │
  735. │       ├────────┴──────────────────────────────────────────────────────┤
  736. │       │ (0-127) within the current block.  This field and the Current │
  737. │       │ Block field at offset 0Ch make up the record pointer.  This   │
  738. │       │ field is not initialized by the OPEN (0Fh) function call.     │
  739. │       │ You must set this field before doing sequential read-write    │
  740. │       │ operations to the diskette.  To read the first record of a    │
  741. │       │ file, set this value to zero.                                 │
  742. ├───────┼─────────┬─────────────────────────────────────────────────────┤
  743. │ 21-25 │ 4 bytes │ Relative Record.                                    │
  744. │       ├─────────┴─────────────────────────────────────────────────────┤
  745. │       │ Points to the currently selected record, counting from the    │
  746. │       │ beginning of the file starting with 0.  This field is not     │
  747. │       │ initialized by the OPEN system call.  You must set this field │
  748. │       │ before doing a random read or write to the file.              │
  749. │       │  If the record size is less than 64 bytes, both words are     │
  750. │       │ used.  Otherwise, only the first 3 bytes are used.  Note that │
  751. │       │ if you use the File Control Block at 5Ch in the program       │
  752. │       │ segment, the last byte of the FCB overlaps the first byte of  │
  753. │       │ the unformatted parameter area.                               │
  754. └───────┴───────────────────────────────────────────────────────────────┘
  755.         
  756. note 1) An unopened FCB consists of the FCB prefix (if used), drive 
  757.         number, and filename.ext properly filled in. An open FCB is 
  758.         one in which the remaining fields have been filled in by the 
  759.         CREAT or OPEN function calls. 
  760.      2) Bytes 0-5 and 32-36 must be set by the user program. Bytes 16-
  761.         31 are set by DOS and must not be changed by user programs. 
  762.      3) All word fields are stored with the least significant byte 
  763.         first. For example, a record length of 128 is stored as 80h at 
  764.         offset 14, and 00h at offset 15. 
  765.  
  766.   Oddly, when Microsoft added the handle calls to DOS they still left 
  767. the old FCB calls with a few advantages.  Handle calls still can't 
  768. read volume labels or directory files and don't support wild card 
  769. deletions.  Microsoft has always claimed that they would eventually 
  770. fix these weaknesses, but we're up to DOS 6.0 and it isn't soup yet. 
  771.  
  772.   Microsoft has always hinted that they would eventually drop FCB 
  773. support from DOS.  Since many compiler runtime libraries depend on FCB 
  774. calls since they are the only ones portable across all DOS versions, a 
  775. lot of existing (expensive) commercial software would no longer run.  
  776. Also note that even the OS/2 Compatibility Box supports FCB calls. 
  777.  
  778.   Many of the handle calls are simply "front ends" for the existing 
  779. FCB code anyway.  It's unlikely the FCB calls will ever vanish from 
  780. DOS. 
  781.  
  782.  
  783. Extended File Control Block ..................................... 7**16
  784.  
  785.   The extended file control block is used to create or search for 
  786. files in the disk directory that have special attributes. 
  787.  
  788.   It adds a 7 byte prefix to the FCB, formatted as follows:
  789.  
  790. ┌────────────────────────────────────────────────────────────────────────┐
  791. │    E X T E N D E D     F I L E      C O N T R O L      B L O C K       │
  792. ├───────┬─────────┬──────────────────────────────────────────────────────┤
  793. │Offset │   Size  │                      Function                        │
  794. ├───────┼─────────┼──────────────────────────────────────────────────────┤
  795. │  00h  │ 1 byte  │ Flag byte containing 0FFh to indicate an extended FCB│
  796. ├───────┼─────────┼──────────────────────────────────────────────────────┤
  797. │  01h  │ 4 bytes │ Reserved by Microsoft                                │
  798. ├───────┼─────────┼──────────────────────────────────────────────────────┤
  799. │  06h  │ 2 bytes │ Attribute byte                                       │
  800. │       ├─────┬───┼──────────────────────────────────────────────────────┤
  801. │       │ hex │bit│                       meaning                        │
  802. │       ├─────┼───┼──────────────────────────────────────────────────────┤
  803. │       │ 00h │   │ (no bits set) normal; can be read or written without │
  804. │       │     │   │ restriction                                          │
  805. │       │ 01h │ 0 │ file is marked read-only.  An attempt to open the    │
  806. │       │     │   │ file for output using int 21h/fn 3Dh will fail and   │
  807. │       │     │   │ an error code will be returned.  This value can be   │
  808. │       │     │   │ used with other values below.                        │
  809. │       │ 02h │ 1 │ indicates a hidden file.  The file is excluded from  │
  810. │       │     │   │ normal directory searches.                           │
  811. │       │ 04h │ 2 │ indicates a system file.  The file is excluded from  │
  812. │       │     │   │ normal directory searches.                           │
  813. │       │ 08h │ 3 │ indicates that the entry contains the volume label   │
  814. │       │     │   │ in the first 11 bytes.  The entry has no other       │
  815. │       │     │   │ usable information and may exist only in the root    │
  816. │       │     │   │ directory.                                           │
  817. │       │ 10h │ 4 │ indicates that the file is a subdirectory            │
  818. │       │ 20h │ 5 │ indicates an archive bit.  This bit is set to on     │
  819. │       │     │   │ whenever the file is written to and closed.  Used by │
  820. │       │     │   │ BACKUP and RESTORE.                                  │
  821. │       │     │ 6 │ reserved, set to 0                                   │
  822. │       │     │ 7 │ reserved, set to 0                                   │
  823. │       ├─────┴───┴──────────────────────────────────────────────────────┤
  824. │       │ note 1) Bits 6 and 7 may be used in OS/2.                      │
  825. │       │ note 2) Attributes 08h and 10h cannot be changed using         │
  826. │       │         int21/43h.    │                                        │
  827. │       │ note 3) The system files IBMBIO.COM and IBMDOS.COM (or         │
  828. │       │         customized equivalent) are marked as read-only, hidden,│
  829. │       │         and system  files.  Files can be marked hidden when    │
  830. │       │         they are created.                                      │
  831. │       │ note 4) Read-only, hidden, system and archive attributes may   │
  832. │       │         be changed with int21h/fn43h.                          │
  833. │       │                                                                │
  834. │       │ Refer to int 21h/fn11h (Search First) for details on using the │
  835. │       │ attribute bits during directory searches.  This function is    │
  836. │       │ present to allow applications to define their own files as     │
  837. │       │ hidden (and thereby excluded from normal directory searches)   │
  838. │       │ and to allow selective directory searches                      │
  839. └───────┴────────────────────────────────────────────────────────────────┘
  840.  
  841.   Any reference in the DOS function calls to an FCB, whether opened or 
  842. unopened, may use either a normal or extended FCB.  If you are using 
  843. an extended FCB, the appropriate register should be set to the first 
  844. byte of the prefix, rather than the drive-number field. 
  845.  
  846.   Common practice is to refer to the extended FCB as a negative offset 
  847. from the first byte of a standard File Control Block. 
  848.  
  849.  
  850. Disk Transfer Area .............................................. 7**17
  851.  
  852.   The old-style (DOS 1.x compatible) FCB-oriented function calls use a 
  853. buffer called the Disk Transfer Area (DTA) for disk access.  Use of 
  854. the DTA is documented in Chapter 6, "DOS Memory Control Blocks and 
  855. Work Areas." 
  856.  
  857.  
  858.